package 查找算法.二分查找;


public class BinarySearch {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        int index = binarySearch(array, 0, array.length - 1, 8);
        System.out.println(index);
    }

    private static int binarySearch(int[] array, int l, int r, int key) {
        int m = (l + r) / 2;
        if (l > r){
            return -1;
        }
        if (array[m] == key) {
            return m;
        }else if (array[m] < key){
            return binarySearch(array,m + 1,r,key);
        }else {
            return binarySearch(array,l,m - 1, key);
        }
    }
}
